#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

/*
 
So currently Carolyn and I are working on functions that will convert the binary chunks 
that are given to us into the register strings.

Currently we made fake values to test our functions out. 
Right now, they will accept binary and conver them into a decimal and add a '$' in front
of it.

We are in the process of converting the main function into a seperate function that you can 
call.

				***UPDATE***

So Carolyn and I met with the professor and discuessed our program. We are in the process of changing the program to 
include:

- sprintf instead of the Itoa
- to change the registers to allow letters ie $a04 $f21
- to skip the step of converting it into a string 

Currently our code still functions and outputs. 
*/








/*

Itoa is not provided in the main stdlib.h as it should be. We found this code in public 
domain to the public.

Itoa automatically converts ints into strings.

			***UPDATE***

We are in the process of removing this function and adding in sprintf.

*/

char* Itoa(int value, char* str, int radix) 
{
	static char dig[] =
	"0123456789"
	"abcdefghijklmnopqrstuvwxyz";
	int n = 0, neg = 0;
	unsigned int v;
	char* p, *q;
	char c;
 
	if (radix == 10 && value < 0) 
	{
		value = -value;
		neg = 1;
	}
	v = value;

	do 
	{
		str[n++] = dig[v%radix];
		v /= radix;
	} 
	while (v);
	if (neg)
	str[n++] = '-';
	str[n] = '\0';
 
	for (p = str, q = p + (n-1); p < q; ++p, --q)
		c = *p, *p = *q, *q = c;
	return str;
}

int binaryToDecimal(char inputString[32])
{
	int len, position;
	double counter;
	int number = 0;
	char *ptr;
	
	len = strlen(inputString);
	len--;
	position = len - 1;
	printf("pos: %d len: %d\n", position, len); 
	ptr = &inputString[0];

	for(counter = 0; counter < len; counter++)
	{

		if(*ptr == '0')
		{
			number = number;
		}
		if(*ptr == '1')
		{
			number = number + pow(2, position);
		}
		position = position - 1;
		ptr++;
		
	}
		 
	printf("Final number: %d\n", number);
	return number;
}

char registerConv(int number)
{
        char dummy[1];
        char stringB4[33];
        char dollar[10] = "$";
        strcat(dollar,Itoa(number,stringB4,10));
        dummy[0] = dollar;
        return dummy[0];

}


int main()
{
        char string1[5] = "00111";
	char string2[5] = "10100";
        char stringF[10];
        char stringtmp[10];
	char stringtmp2[10];
        char entireString[6] = "$";
	char entireString2[6] = "$";
        int num;
	int num2;
        int binary;     

        num = binaryToDecimal(string1); 
        Itoa(num, stringtmp, 10);
        strcat(entireString, stringtmp);
        printf("The new string is: %s\n", entireString);

	num2 = binaryToDecimal(string2);
	Itoa(num2, stringtmp2, 10);
	strcat(entireString2, stringtmp2);
	printf("The new string is %s\n", entireString2);

        return 0;
}
